package com.hcrazy.manchester.app.controllers.coupon;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.form.CouponSearchForm;
import com.hcrazy.manchester.app.pojo.BookMenu;
import com.hcrazy.manchester.app.pojo.Coupon;
import com.hcrazy.manchester.app.pojo.CouponSeller;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.CouponSellerService;
import com.hcrazy.manchester.app.services.CouponService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/coupon")
public class CouponController {
	
	@Autowired
	private CouponService couponService;
	
	@Autowired
	private CouponSellerService couponSellerService;
	
	@Autowired
	private BookMenuService bookMenuService;
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addCoupon",method=RequestMethod.GET)
	public void addCoupon(Model model){
		//设置分类
		setSellerSorts(model);
		//初始化新增对象
		model.addAttribute("coupon", new Coupon());
	}
	
	/**
	 * 编辑信息内容
	 * @param sellerId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/editCoupon",method=RequestMethod.GET)
	public String editCoupon(@RequestParam int couponId,Model model){
		try {
			//初始化对象内容
			addCoupon(model);
			//根据id获取信息内容
			Coupon coupon = couponService.getInfoById(couponId);
			//更新返回对象
			model.addAttribute("coupon",coupon);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/coupon/addCoupon";
	}
	
	/**
	 * 提交信息内容
	 * @param seller
	 * @return
	 */
	@RequestMapping(value="/coupon",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateCoupon(@ModelAttribute("form") Coupon coupon,
    		Model model){
		try {
			couponService.addOrUpdateCoupon(coupon);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/coupon/coupon";
    }
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/coupon",method=RequestMethod.GET)
	public void coupon_seller(Model model){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllCoupons(1, 0, "",0);
		model.addAttribute("pageModel", pageModel);
		
		//初始化分类
		setSellerSorts(model);
		
		//初始化查询对象
		model.addAttribute("searchForm", new CouponSearchForm());
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_coupon",method=RequestMethod.POST)
	public String search(@ModelAttribute("form") CouponSearchForm form,Model model){
		//是否执行删除操作
		if(form.getCouponId() > 0){
			try {
				couponService.delCoupon(form.getCouponId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置sellerId
		form.setCouponId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllCoupons(form.getPageNum(), form.getSortId(), form.getName(),form.getSellerId());
		model.addAttribute("pageModel", pageModel);

		//设置查询类别
		setSellerSorts(model);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/coupon/coupon";
	}
	
	@RequestMapping(value="/sellers",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<CouponSeller> getSellersBysortId(@RequestParam int sortId){
		try {
			return couponSellerService.getAllConponSellersBySortId(sortId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllCoupons(int pageNum,int sortId,String name,int sellerId){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return couponService.getAllCoupons(model, name, sortId,sellerId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 按功能获取分类并放在在model对象中
	 * @param model
	 */
	private void setSellerSorts(Model model){
		//根据功能码设置分类列表
		try {
			List<BookMenu> sorts = bookMenuService.getAllMenus(Constant.TYPE_COUPON, 0);
			model.addAttribute("sorts", sorts);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}		
	}
}
